//
//  RegisterHeaderView.m
//  Demo
//
//  Created by LG on 2018/10/16.
//  Copyright © 2018 LG. All rights reserved.
//

#import "RegisterHeaderView.h"


@interface RegisterHeaderView ()


@end

@implementation RegisterHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        [self buildUI];
    }
    return self;
}

- (void)buildUI
{
    self.layer.masksToBounds = YES;
    CGFloat y = 30.;
    
    CGRect imgViewPhotoBounds = self.imgView_userPhoto.bounds;
    self.imgView_userPhoto.frame = CGRectMake(20.,y, CGRectGetWidth(imgViewPhotoBounds), CGRectGetHeight(imgViewPhotoBounds));
    
    CGRect btnDetailBounds = self.btn_detail.bounds;
    self.btn_detail.frame = CGRectMake(kScreenWidth-CGRectGetWidth(btnDetailBounds),
                                       y, CGRectGetWidth(btnDetailBounds), CGRectGetHeight(btnDetailBounds));

    [self updateGrade:@"0"];
    CGFloat gradeX = CGRectGetMaxX(self.imgView_userPhoto.frame) + 10;
    CGFloat gradeWidth = kScreenWidth - gradeX - self.btn_detail.frame.size.width;
    self.lbl_grade.frame = CGRectMake(gradeX, y, gradeWidth, 22.);
    
    [self updateRank:@"等级" rankPhoto:@"jifen_jiantou"];
    CGFloat rankY = CGRectGetMaxY(self.lbl_grade.frame) + 5;
    CGRect rankBounds = self.btn_rank.bounds;
    rankBounds.origin = CGPointMake(gradeX, rankY);
    self.btn_rank.frame = rankBounds;
    
//    self.view_category.frame = CGRectMake(0, CGRectGetMaxY(self.calendarView.frame), kScreenWidth, [[self class] heightForOriginal]-CGRectGetMaxY(self.calendarView.frame));
    
    [self addSubview:self.imgView_headerBG];
    [self addSubview:self.imgView_userPhoto];
    [self addSubview:self.lbl_grade];
    [self addSubview:self.btn_rank];
    [self addSubview:self.btn_detail];
    [self addSubview:self.calendarView];
//    [self addSubview:self.view_category];
}

#pragma mark -- Method

+ (CGFloat)heightForOriginal
{
    return 259.;
}

+ (CGFloat)heightForHadSigned
{
    return 212.;
}

- (CGFloat)heightForNow
{
    if (1) {
        return [[self class]heightForOriginal];
    }
    
    return [[self class]heightForHadSigned];
}

- (void)updateGrade:(NSString*)grade
{
    NSMutableAttributedString *attributed = [[NSMutableAttributedString alloc]initWithString:@"你的可用积分:  "
                                                                                  attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:15],
                                                                                               NSForegroundColorAttributeName:[UIColor whiteColor]}];
    NSAttributedString *gradeAttributed = [[NSAttributedString alloc]initWithString:grade attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:19],
                                                                                                       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [attributed appendAttributedString:gradeAttributed];
    
    self.lbl_grade.attributedText = attributed;
}

- (void)updateRank:(NSString*)rankName rankPhoto:(NSString*)rankPhoto
{
    if (!rankName || ![rankName isKindOfClass:[NSString class]] ) {
        return;
    }
    
    [self.btn_rank setTitle:rankName forState:UIControlStateNormal];
    [self.btn_rank.titleLabel sizeToFit];
    
    UIImage *rankImage = [UIImage imageNamed:rankPhoto];
    UIImageView *imgView_rank = [self.btn_rank viewWithTag:1];
    if (!imgView_rank) {
        imgView_rank = [[UIImageView alloc]initWithImage:rankImage];
        imgView_rank.tag = 1;
        imgView_rank.frame = CGRectMake(10, 4, rankImage.size.width, rankImage.size.height);
        [self.btn_rank addSubview:imgView_rank];
    }
    
    UIImage *imgMore = [UIImage imageNamed:@"jifen_jiantou"];
    [_btn_rank setImage:imgMore forState:UIControlStateNormal];
    self.btn_rank.titleEdgeInsets = UIEdgeInsetsMake(0, -imgMore.size.width-2, 0,imgMore.size.width);
    self.btn_rank.imageEdgeInsets = UIEdgeInsetsMake(0, _btn_rank.titleLabel.frame.size.width + 3, 0, -_btn_rank.titleLabel.frame.size.width);
    self.btn_rank.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0.);
    
    [self.btn_rank sizeToFit];
    CGRect btnRankFrame = self.btn_rank.frame;
    btnRankFrame.size.width += rankImage.size.width + 10;;
    self.btn_rank.frame = btnRankFrame;
}

- (void)buildRankBtnWithRankText:(NSString*)rankText rankPhoto:(NSString*)rankPhoto
{
    UIImage *imageRank = [UIImage imageNamed:rankPhoto];
    UIImageView *imgViewRank = [self.btn_rank viewWithTag:1];
    if (!imgViewRank) {
        imgViewRank = [[UIImageView alloc]init];;
        imgViewRank.tag = 1;
        [self.btn_rank addSubview:imgViewRank];
    }
    imgViewRank.image = imageRank;
    
    UILabel *lblRank = [self.btn_rank viewWithTag:2];
    if (!lblRank) {
        lblRank = [[UILabel alloc]init];
        lblRank.tag = 2;
        lblRank.textColor = [UIColor whiteColor];
        lblRank.font = [UIFont systemFontOfSize:12];
        [self.btn_rank addSubview:lblRank];
    }
    lblRank.text = rankText;
    [lblRank sizeToFit];
    
    UIImageView *imgViewMore = [self.btn_rank viewWithTag:3];
    if (!imgViewMore) {
        UIImage *imageMore = [UIImage imageNamed:@""];
        imgViewMore = [[UIImageView alloc]initWithImage:imageMore];
        imgViewMore.tag = 3;
        [self.btn_rank addSubview:imgViewMore];
    }
    CGRect imgViewMoreFrame = imgViewMore.frame;
    imgViewMoreFrame.origin.x = CGRectGetMaxX(lblRank.frame);
    imgViewMore.frame = imgViewMoreFrame;
    
    CGRect btnRankFrame = self.btn_rank.frame;
    btnRankFrame.size.width = CGRectGetMaxX(imgViewMoreFrame);
}

#pragma mark -- Getter
- (UIImageView *)imgView_headerBG
{
    if (!_imgView_headerBG) {
        _imgView_headerBG = [[UIImageView alloc]init];
        UIImage *img = [UIImage imageNamed:@"jifen_bj"];
        _imgView_headerBG.image = img;
        CGFloat width = CGRectGetWidth([UIScreen mainScreen].bounds);
        CGFloat height = 157;
        _imgView_headerBG.frame = CGRectMake(0, 0, width, height);
    }
    
    return _imgView_headerBG;
}

- (UIImageView *)imgView_userPhoto
{
    if (!_imgView_userPhoto) {
        _imgView_userPhoto = [[UIImageView alloc]init];
        UIImage *img = [UIImage imageNamed:@"jifen_touxiang"];
        _imgView_userPhoto.image = img;
        _imgView_userPhoto.bounds = CGRectMake(0, 0, 44., 44.);
    }
    
    return _imgView_userPhoto;
}

- (UILabel *)lbl_grade
{
    if (!_lbl_grade) {
        _lbl_grade = [[UILabel alloc]init];
        _lbl_grade.textColor = [UIColor whiteColor];
        _lbl_grade.font = [UIFont boldSystemFontOfSize:14.];
    }
    
    return _lbl_grade;
}

- (UIButton *)btn_rank
{
    if (!_btn_rank) {
        _btn_rank = [UIButton buttonWithType:UIButtonTypeCustom];
        _btn_rank.backgroundColor = [UIColor colorWithRed:226./255 green:180./255 blue:111./255 alpha:1.];
        _btn_rank.layer.cornerRadius = 3;
        _btn_rank.layer.masksToBounds = YES;
    
        _btn_rank.titleLabel.textColor = [UIColor whiteColor];
        _btn_rank.titleLabel.font = [UIFont systemFontOfSize:12];
    }
    
    return _btn_rank;
}

- (UIButton *)btn_detail
{
    if (!_btn_detail) {
        _btn_detail = [UIButton buttonWithType:UIButtonTypeCustom];
        _btn_detail.backgroundColor = [UIColor whiteColor];
        CGFloat btnHeight = 32.;
        CGFloat btnWidth = 72.;
        _btn_detail.frame = CGRectMake(0, 0., btnWidth, btnHeight);
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_btn_detail.bounds
                                                       byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerTopLeft
                                                             cornerRadii:CGSizeMake(btnHeight/2,btnHeight/2)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = _btn_detail.bounds;
        maskLayer.path = maskPath.CGPath;
        _btn_detail.layer.mask = maskLayer;
        
        UIImage *imgDetail = [UIImage imageNamed:@"jifen_mingxi"];
        [_btn_detail setImage:imgDetail forState:UIControlStateNormal];
        _btn_detail.imageView.frame = CGRectMake(0, 0, imgDetail.size.width, imgDetail.size.height);
        
        _btn_detail.titleLabel.font = [UIFont systemFontOfSize:12. weight:UIFontWeightBold];
        [_btn_detail setTitleColor:[UIColor colorWithRed:238./255. green:183./255. blue:94./255. alpha:1.] forState:UIControlStateNormal];
        [_btn_detail setTitle:@"明细" forState:UIControlStateNormal];
        
        _btn_detail.titleEdgeInsets = UIEdgeInsetsMake(0, imgDetail.size.width-2, 0, 0);
        _btn_detail.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    
    return _btn_detail;
}

- (CalendarView *)calendarView
{
    if (!_calendarView) {
        _calendarView = [[CalendarView alloc]initWithFrame:CGRectMake(15., 120, kScreenWidth-15.*2, [CalendarView viewHeightWithFold:NO])];
    }
    
    return _calendarView;
}

- (UIView *)view_category
{
    if (!_view_category) {
        _view_category = [UIView new];
        _view_category.backgroundColor = [UIColor redColor];
    }
    
    return _view_category;
}

@end
